package com.vf.admin.miaodong.controller;

import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.common.BaseController;
import com.vf.admin.common.Constants;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppVerSion;
import com.vf.admin.miaodong.service.AfterVerSionService;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户端版本管理
 */
@Controller
@RequestMapping("/after/version")
public class AfterVersionController extends BaseController{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    AfterVerSionService appVerSionService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 版本列表
     * @param splitPageBean
     * @param updatestate 是否强制更新 1、是  2、否  默认2、否
     * @param state   是否生效   1、是  2、否  默认2、否
     * @param qudaoid  渠道 1、安卓  2、ios
     * @param vername  版本名称
     * @return
     */
    @RequestMapping("/list")
    public String list(SplitPageBean splitPageBean,Integer updatestate,Integer state,Integer qudaoid,String vername){
        try {
            Map<String,Object> map = new HashMap<>();
            map.put("updatestate",updatestate);
            map.put("state",state);
            map.put("qudaoid",qudaoid);
            map.put("vername",vername);
            List<AppVerSion> list = appVerSionService.find(map,splitPageBean);
            //是否生效
            Map<Integer,String> stateMap = Constants.getShengXiaoStateMap();
            request.setAttribute("stateMap",stateMap);
            //渠道
            Map<Integer,String> getQdMap = Constants.getQdMap();
            request.setAttribute("getQdMap",getQdMap);
            //强制更新
            Map<Integer,String> qzgxMap = Constants.getAppUpdateStateMap();
            request.setAttribute("qzgxMap",qzgxMap);

            request.setAttribute("list",list);
            request.setAttribute("splitPage",splitPageBean);
            request.setAttribute("updatestate",updatestate);
            request.setAttribute("state",state);
            request.setAttribute("qudaoid",qudaoid);
            request.setAttribute("vername",vername);
            return this.sucess("/web/after/version/list");
        }catch (Exception e){
            logger.error("版本更新列表异常",e);
            return this.error("系统繁忙");
        }
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @RequestMapping("/detail")
    public String detail(Integer id){
        try{
            AppVerSion version = null;
            if (id!=null && id>0) {
                version = appVerSionService.getId(id);
            }
            request.setAttribute("version",version);
            //是否生效
            Map<Integer,String> stateMap = Constants.getShengXiaoStateMap();
            request.setAttribute("stateMap",stateMap);
            //渠道
            Map<Integer,String> getQdMap = Constants.getQdMap();
            request.setAttribute("getQdMap",getQdMap);
            //强制更新
            Map<Integer,String> qzgxMap = Constants.getAppUpdateStateMap();
            request.setAttribute("qzgxMap",qzgxMap);

            return  this.sucess("/web/after/version/edit");
        }catch (Exception e){
            logger.error("查看版本详情异常",e);
            return this.error("系统繁忙");
        }
    }

    /**
     * 编辑版本信息
     * @param appVerSion
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public Result save(AppVerSion appVerSion){
        try{
            if(appVerSion == null){
                return this.errorJSON("版本信息不能为空");
            }
            if(appVerSion!=null && appVerSion.getId()!=null && appVerSion.getId()>0){
                AppVerSion verSion = appVerSionService.getId(appVerSion.getId());
                if(verSion == null){
                    return this.errorJSON("未获取到相关信息");
                }
                verSion.setContent(appVerSion.getContent());
                verSion.setQudaoid(appVerSion.getQudaoid());
                verSion.setState(appVerSion.getState() == null ? Constants.shiFou_STATE_FOU : Constants.shiFou_STATE_SHI);
                verSion.setUpdatestate(appVerSion.getUpdatestate() == null ? Constants.shiFou_STATE_FOU : Constants.shiFou_STATE_SHI);
                verSion.setVerint(appVerSion.getVerint());
                verSion.setVername(appVerSion.getVername());
                verSion.setUrl(appVerSion.getUrl());
                appVerSionService.save(verSion);
            }else{
                appVerSionService.save(appVerSion);
            }

            //修改版本信息为有效状态时，把版本信息存到redis中
            Map paraMap = new HashMap();
            paraMap.put("state", Constants.SHENGXIAO_STATE_1);
            paraMap.put("qudaoid", Constants.QUDAO_GUANWANG);
            appVerSion = appVerSionService.latestVersion(paraMap);
            if(appVerSion==null || appVerSion.getId()==null || appVerSion.getId()<=0){
                appVerSion = new AppVerSion();
            }
            redisUtil.set(RedisKeys.CACHE_VERSION, appVerSion);

            return this.sucessJSON("保存成功");
        }catch (Exception e){
            logger.error("编辑版本信息异常",e);
            return  this.errorJSON("系统繁忙");
        }
    }

    /**
     * 删除版本信息
     * @param idArray
     * @return
     */
    @RequestMapping("/del")
    @ResponseBody
    public Result del(String idArray){
        try{
            if(StringEx.isNull(idArray)){
                return  this.errorJSON("未获取到相关版本信息");
            }
            String[] userIdArray = idArray.split(",");
            if(userIdArray!=null && userIdArray.length>0){
                for(String id : userIdArray){
                    if(StringEx.isNull(id)){
                        continue;
                    }
                    AppVerSion version = appVerSionService.getId(Integer.valueOf(id));
                    if(version==null ||  version.getId()==null || version.getId()<=0){
                        return this.errorJSON("版本信息不存在!");
                    }else{
                        appVerSionService.del(version.getId());
                    }
                }
            }
            return this.sucessJSON("删除成功");
        }catch (Exception e){
            logger.error("删除版本信息异常",e);
            return  this.errorJSON("系统繁忙");
        }
    }

}
